<?php
/**
 * XGPHP 轻量级PHP框架
 * @link http://xgphp.xg3.cn
 * @version 2.0.0
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @author 讯高科技 <xungaokeji@qq.com>
*/
namespace xg;
class Model extends Database {
	protected $name = '';
	protected $error = null;
	protected $validator = null;
	protected $validate = [];
	protected function connect(string $type='') {
		if($this->name){
			$this->name($this->name);
		}
		return parent::connect($type);
	}
	// 插入记录
	public function insert(array $data=[]){
		if(!$this->validate($data)) {
			throw new \xg\Error($this->error);
			return false;
		}
		$data = \helper\Safe::data($data);
		return parent::insert($data);
	}
	// 插入记录
	public function update(array $data=[]){
		if(!$this->validate($data)) {
			throw new \xg\Error($this->error);
			return false;
		}
		$data = \helper\Safe::data($data);
		return parent::update($data);
	}
	protected function validate(array $data=[]) {
		if ($this->validator) {
			$validator = new $this->validator;
		} elseif($this->validate) {
			$validator = new \xg\Validate($this->validate);
		}
		if($validator and !$validator->check($data)) {
			$this->error = $validator->getError();
			return false;
		}
		return true;
	}
	public function getError() {
		return $this->error;
	}
}
?>